<?php

namespace App\Http\Controllers\Page\Admin;

use App\Facades\UserService;
use App\Facades\WxUserService;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index(Request $request)
    {
        $user = UserService::getLoginUser();
        $adminInfoList = UserService::getAllAdminInfo($user);
        return view('admin/account_manage')->with([
            'adminInfoList' => $adminInfoList,
            'user' => $user,
            ]);
    }

    public function login(Request $request)
    {
        return view('admin.login');
    }

    public function add(Request $request)
    {
        return view('admin/account_add');
    }

    public function edit(Request $request)
    {
        $rules = [
            'userId' => 'required|exists:users,id',
        ];
        $this->validate($request, $rules);

        $adminInfo = UserService::findAdminInfoById($request->userId);
        return view('admin/account_edit')->with(['adminInfo' => $adminInfo]);
    }

    public function userInfoList(Request $request)
    {
        $openId = $request->username;
        $openId = str_replace('%', '\%', $openId);
        $openId = str_replace('_', '\_', $openId);
    	$userInfoList = WxUserService::getWxUserListWithUserInfo($openId, $request->phone);
        return view('admin/user_info')->with([
            'userInfoList' => $userInfoList,
            'username' => $request->username,
            'phone' => $request->phone,
            ]);
    }

    public function updatePassword(Request $request)
    {
        return view('admin/alter_password');
    }

}
